import xadmin
from .models import *
from organization.models import CourseOrg

class LessonInLine(object):
    model = Lesson
    extra = 0
class CourseRecourseInline(object):
    model = CourseResorce
    extra = 0
class CourseAdmin(object):
    list_display = ('name','desc','detail','degree','learn_times','students','zj_nums')
    search_fields = ('name','desc','detail','degree','students')
    list_filter = ('name','desc','detail','degree','learn_times','students')
    ordering = ['-click_num']
    readonly_fields = ['favor_nums']
    exclude = ['click_num']
    list_editable = ['degree','desc']
    inlines = [LessonInLine,CourseRecourseInline]
    refresh_times = [15,30,45,60,120,180,300,500]

    def queryset(self):
        qs = super(CourseAdmin,self).queryset()
        qs = qs.filter(is_banner = False)
        return qs

    def save_models(self):
        #保存课程的时候统计课程机构的课程数
        obj = self.new_obj
        obj.save()
        if obj.course_org is not  None:
            course_org = obj.course_org
            course_org.course_nums = Course.objects.filter(course_org=course_org).count()
            course_org.save()

class BannerCourseAdmin(object):
    list_display = ('name','desc','detail','degree','learn_times','students')
    search_fields = ('name','desc','detail','degree','students')
    list_filter = ('name','desc','detail','degree','learn_times','students')
    ordering = ['-click_num']
    readonly_fields = ['favor_nums']
    exclude = ['click_num']
    inlines = [LessonInLine,CourseRecourseInline]

    def queryset(self):
        qs = super(BannerCourseAdmin,self).queryset()
        qs = qs.filter(is_banner = True)
        return qs


class LessonAdmin(object):
    list_display = ('course','name','add_time')
    search_fields = ('course','name')
    list_filter = ('course__name','name','add_time')
class VideoAdmin(object):
    list_display = ('lesson','name','add_time')
    search_fields = ('lesson','name')
    list_filter = ('lesson','name','add_time')
class CourseResorceAdmin(object):
    list_display = ('course','name','download','add_time')
    search_fields = ('course','name','download')
    list_filter = ('course','name','download','add_time')

xadmin.site.register(Course,CourseAdmin)
xadmin.site.register(BannerCourse,BannerCourseAdmin)
xadmin.site.register(Lesson,LessonAdmin)
xadmin.site.register(Video,VideoAdmin)
xadmin.site.register(CourseResorce,CourseResorceAdmin)